package org.asiainfo.flowable.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.flowable.domain.vo.WfFormVo;
import org.asiainfo.flowable.domain.bo.WfFormBo;
import org.asiainfo.flowable.domain.WfForm;
import org.asiainfo.flowable.service.WfFormService;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.flowable.mapper.WfFormMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.Collection;
import java.util.List;

/**
 * 流程表单信息表(WfForm)表服务实现类
 *
 * @author dotor-ww
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class WfFormServiceImpl extends ServiceImpl<WfFormMapper, WfForm> implements WfFormService {

    /**
     * 构建查询条件
     *
     * @param wfFormBo 流程表单
     * @return 构建结果
     */
    private QueryWrapper buildQueryWrapper(WfFormBo wfFormBo) {
        QueryWrapper query = query();
        query.like(WfForm::getFormName, wfFormBo.getFormName(), StringUtils.isNotBlank(wfFormBo.getFormName()));
        return query;
    }

    /**
     * 查询
     *
     * @param wfFormBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<WfFormVo> queryList(WfFormBo wfFormBo) {
        QueryWrapper query = buildQueryWrapper(wfFormBo);
        return listAs(query, WfFormVo.class);
    }

    /**
     * 通过formId查询单条数据
     *
     * @param formId 主键
     * @return 实例对象
     */
    @Override
    public WfFormVo queryById(Long formId) {
        return getOneAs(query().eq(WfForm::getFormId, formId), WfFormVo.class);
    }

    /**
     * 分页查询
     *
     * @param wfFormBo  筛选条件
     * @param pageQuery 分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<WfFormVo> queryPageList(WfFormBo wfFormBo, PageQuery pageQuery) {
        QueryWrapper query = buildQueryWrapper(wfFormBo);
        Page<WfFormVo> page = pageAs(pageQuery.buildPage(), query, WfFormVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param wfFormBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(WfFormBo wfFormBo) {
        WfForm wfForm = MapstructUtils.convert(wfFormBo, WfForm.class);
        return save(wfForm);
    }

    /**
     * 修改数据
     *
     * @param wfFormBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateByBo(WfFormBo wfFormBo) {
        WfForm wfForm = MapstructUtils.convert(wfFormBo, WfForm.class);
        return updateById(wfForm);
    }

    /**
     * 通过主键删除数据
     *
     * @param formIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> formIds) {
        return removeByIds(formIds);
    }

    /**
     * 校验表单名称是否唯一
     *
     * @param wfFormBo 实例对象
     */
    @Override
    public Boolean checkWfFormNameUnique(WfFormBo wfFormBo) {
        return exists(query().eq(WfForm::getFormName, wfFormBo.getFormName())
            .ne(WfForm::getFormId, wfFormBo.getFormId(), ObjectUtil.isNotNull(wfFormBo.getFormId())));
    }
}
